<?php
namespace app\home\controller;
header("Content-type: text/html; charset=utf-8");
use app\admin\model\Account;
use app\admin\model\StudentWithdrawals;
use app\admin\model\Order;
use think\Cache;
use think\Controller;
use think\Db;
use think\Validate;
use app\admin\model\User;

class Student extends Controller{
//获取奖励学币
    public function month_salary(){
        if(request()->isPost()){
            $info = Db::name('coin')->where(array('receive_id'=>input('post.receive_id')))->find();
            $coins = $info['month_coin'];
            $true['status'] = 1;
            $true['coin'] = $coins;
            $false['status'] = 0;
            $empty['status'] = 2;
            $students = Db::name('student');
            $student = $students->where(array('student_id'=>input('post.receive_id')))->find();
            if($info['month_coin']!=0){
                $coin['coin']=$student['coin']+$info['month_coin'];
                $get_coin =$students->where(array('student_id'=>input('post.receive_id')))->update($coin);
                if($get_coin){
                    //echo '<script>alert(\'恭喜你成功领取奖励学币\');</script>';
                    $data['account_id'] = input("post.receive_id");
                    $data['cost_description']= '领取奖励学币  +'.$info['month_coin'];
                    $data['record_time']=time();
                    $data['cost_type']=1;
                    $rec=Db::name('account')->insert($data);
                    $true['status'] = 1;
                    $false['status'] = 0;
                    if($rec){
                        //echo '<script>alert(\'流水新增成功\');</script>';
                        $info['month_coin']=0;
                        $rec = Db::name('coin')->where(array('receive_id'=>input('post.receive_id')))->update($info);
                        if($rec){
                            return json($true);
                        }else{
                            return json($false);
                        }
                    }else{
                        return json($false);
                    }

                }else{
                    return json($false);
                }
            }else{
                return json($empty);

            }
        }
    }
    //捐书领书学币
    public function donation_receive(){//前台回传id 和donation_id
        if(request()->isPost()){
            $true['status'] = 1;
            $false['status'] = 0;
            $id =$_POST['id'];
            $donation_id = $_POST['donation_id'];
            $students = Db::name('student');
            $student = $students->where(array('student_id'=>input('post.donation_id')))->find();
            $info =Db::name('donation')->where(array('id'=>$id,'donation_id'=>$donation_id))->find();
            if($info['is_receive']==0){
                $info['is_receive'] = 1;
                $rec =Db::name('donation')->where(array('id'=>$id,'donation_id'=>$donation_id))->update($info);
                if($rec){
                    //echo '<script>alert(\'恭喜你成功领取本书学币\');</script>';
                    $coin['coin']=$student['coin']+$info['get_coin'];
                    $get_coin =$students->where(array('student_id'=>input('post.donation_id')))->update($coin);
                    if($get_coin){
                        //echo '<script>alert(\'本书学币成功转入账户\');</script>';
                        $data['account_id'] = input("post.donation_id");
                        $data['cost_description']='领取捐书学币  +'.$info['get_coin'];
                        $data['record_time']=time();
                        $data['cost_type']=1;
                        $rec=Db::name('account')->insert($data);
                        if($rec){
                            return json($info);
                        }else{
                            return json($data);
                        }
                    }else{
                        return json($false);
                    }
                }else{
                    return json($false);
                }
            }else{
                return json($false);
            }


        }
    }
public function self_donation_list(){
    $donation_id = $_POST['donation_id'];
    $info =Db::name('donation')
        ->join('book_user','donation_id=book_user.id','LEFT')
        ->where(array('donation_id'=>$donation_id))
        ->order('book_donation.id desc')
        ->select();
    return json($info);
}
    public function donation_list(){
       // $_POST['donation_id']= 3;
        $donation_id = $_POST['donation_id'];
        $info =Db::name('donation')
            ->where(array('donation_collect_id'=>$donation_id))
            ->join('book_user','donation_id=book_user.id','LEFT')
            ->order('book_donation.id desc')
            ->select();
        return json($info);
    }
    public function rec_teacher(){
        $id = $_POST['id'];
        $info =Db::name('user')->where(array('rec_id'=>$id,'user_auth'=>2))->order('id desc')->select();
        return json($info);
    }
    public function rec_student(){
        $id = $_POST['id'];
        $info =Db::name('user')->where(array('rec_id'=>$id,'user_auth'=>3))->order('id desc')->select();
        return json($info);
    }
    public function rec_classroom(){
    $id = $_POST['id'];
    $info =Db::name('classroom')
          ->where(array('rec_classroom_user'=>$id))
          ->join('book_user','classroom_owner=book_user.id','LEFT')
          ->order('book_classroom.id desc')
          ->field('book_classroom.classroom_code,book_classroom.classroom_address,book_user.full_name')
          ->select();
    return json($info);
}
    public function student_coin(){
        $id = $_POST['id'];
        $info =Db::name('student')->where(array('student_id'=>$id))->field('book_student.coin')->find();
        return json($info);
    }
    public function student_payment(){
        $id = $_POST['id'];
        $info =Db::name('student')->where(array('student_id'=>$id))->field('book_student.tuition')->find();
        return json($info);
    }
    public function student_bonus(){
        $id = $_POST['id'];
        $info =Db::name('user')->where(array('id'=>$id,'user_auth'=>3))->field('book_user.rec_bonus')->find();
        return json($info);
    }
    public function account_list(){
        $id = $_POST['id'];
        $info =Db::name('account')
            ->order('record_time desc')
            ->where(array('account_id'=>$id))
            ->field('book_account.cost_description,book_account.record_time,book_account.cost_type')
            ->select();
        return json($info);
    }

    public function add_Order(){//添加订单
        $status0['status']=0;
        $status1['status']=1;
        $status2['status']=2;
        $status3['status']=3;
        $status4['status']=4;
        if(request()->isPost()){
            $count = Db::name('order')->where('lesson_id=' .$_POST['lesson_id'])->count();
            $led = Db::name('lesson')->where('id='.$_POST['lesson_id'])->find();
            $max_person = $led['max_person'];
            $end_enroll_time = $led['registration_end_time'];
            $lesson_details_id = $_POST['lesson_id'];
            $data['order_lesson_sn'] = $led['lesson_sn'];//获取已审核通过的课程编号
            $data['user_id'] = $_POST['user_id'];
            $time = date('Ymd');//制作订单号
            $order_num = Db::name('order')->select();
            if ($order_num) {
                foreach ($order_num as $k => $v) {
                    $arr[] = substr($order_num[$k]['order_sn'], -4);
                }
                $data['order_sn'] = $time . $led['lesson_sn'] . sprintf("%04d", max($arr) + 1);
            } else {
                $data['order_sn'] = $time . $led['lesson_sn'] . sprintf("%04d", 1);
            }
            $data['order_status'] = 0;
            $data['order_time'] = time();//制作订单时间
            $info = Db::name('lesson')->where(array('lesson_sn' => $data['order_lesson_sn']))->find();
            $data['lesson_id'] = $info['id'];//获取课程详情id
            $time = $end_enroll_time - $data['order_time'];
            if ($time > 0||$end_enroll_time==null) {
                if ($max_person - $count > 0||$max_person==null) {
                    $stu_info = Db::name('student')->where(array('student_id' => $data['user_id']))->find();
                    $student['tuition'] = $stu_info['tuition'] - $info['lesson_cost'];
                    if ($info['cost_type'] == 1) {
                        $stu_info = Db::name('student')->where(array('student_id' => $data['user_id']))->find();
                        $student['tuition'] = $stu_info['tuition'] - $info['lesson_cost'];
                        if ($student['tuition'] >= 0) {
                            $order = Db::name('order');
                                $rec = $order->insert($data);
                                if ($rec) {
                                    Db::name('student')->where(array('student_id' => $data['user_id']))->update($student);
                                    $comment['lesson_id'] = $lesson_details_id; //评论表
                                    $comment['comment_lesson_sn'] = $data['order_lesson_sn'];
                                    $comment['comment_time'] = time();
                                    $comment['comment_rank'] = 1;
                                    $comment['comment_id'] = $data['user_id'];
                                    $comment['comment_status'] = 1;
                                    $comment['ord_id'] = $rec;
                                    $c_r = Db::name('lesson_comment')->insert($comment);
                                    if ($c_r) {
                                        $set['o_id'] = $rec;
                                        $set['lesson_id']=$_POST['lesson_id'];
                                        $settlement = Db::name('settlement');// 结算表
                                        $info_sec = $settlement->insert($set);
                                        if ($info_sec) { //流水表
                                            $account['account_id'] = $data['user_id'];
                                            $account['cost_description'] = '报名课程花费学费  -' . $info['lesson_cost'];
                                            $account['record_time'] = time();
                                            $account['cost_type'] = 2;
                                            $rec_account = Db::name('account')->insert($account);
                                            if ($rec_account) {
                                                return json($status0);//报名成功
                                            } else {
                                                return json($status1);//报名失败

                                            }
                                        }
                                    }
                                }

                        } else {
                            return json($status2);//学费不足
                        }

                    } elseif ($info['cost_type'] == 2) {
                        $stu_info = Db::name('student')->where(array('student_id' => $data['user_id']))->find();
                        $stu_coin['coin'] = $stu_info['coin'] - $info['lesson_cost'];
                        if ($stu_info['coin'] - $info['lesson_cost'] >= 0) {
                            $order = Db::name('order');
                                $rec = $order->insertGetId($data);
                                if ($rec) {
                                    $stu_rec = Db::name('student')->where(array('student_id' => $data['user_id']))->update($stu_coin);
                                    if ($stu_rec) {
                                        $account['account_id'] = $data['user_id'];
                                        $account['cost_description'] = '报名课程花费学币  -' . $info['lesson_cost'];
                                        $account['record_time'] = time();
                                        $account['cost_type'] = 1;
                                        $rec_account = Db::name('account')->insert($account);
                                        if ($rec_account) {
                                            return json($status0);//报名成功
                                        } else {
                                            return json($status1);//报名失败

                                        }
                                    }
                                }


                        } else {
                            return json($status2);//学费不足
                        }
                    }
                } else {
                    return json($status3);//名额已满
                }
            }else{
                return json($status4);//时间已过

            }
        }

    }
    public function my_tuition_now(){
        $orders=Db::name('order');
        $order = $orders
                 ->where('book_order.user_id="'.$_POST['id'].'" AND book_lesson.lesson_num>book_lesson.com_lesson_num AND book_lesson.cost_type=1')
                 ->join('book_lesson','lesson_id=book_lesson.id','LEFT')
            ->join(' book_subject ','subject_id=book_subject.id','LEFT')
            ->join(' book_lesson_program ','program_id=book_lesson_program.id','LEFT')
            ->join(' book_service_target ','service_target_id=book_service_target.id','LEFT')
            ->join(' book_lesson_nature ','lesson_nature_id=book_lesson_nature.id','LEFT')
            ->join(' book_user ','teacher_id=book_user.id','LEFT')
            ->join(' book_classroom ','classroom_id=book_classroom.id','LEFT')
            ->join(' book_city ','book_lesson.city_id=book_city.id','LEFT')
            ->join('book_lesson_comment','user_id=book_lesson_comment.comment_id AND book_order.lesson_id  =book_lesson_comment.lesson_id','LEFT')
            ->join('book_lesson_num_time','book_lesson_num_time.lesson_id=book_lesson.id AND book_lesson_num_time.lesson_number  =book_lesson.lesson_num','LEFT')
            ->field('book_lesson.id,book_lesson.lesson_sn,book_subject.subject,book_lesson_program.program,book_service_target.service_target,book_lesson_nature.lesson_nature,book_user.full_name,book_user.sex,book_user.age,book_classroom.classroom_address,book_lesson.lesson_method,book_lesson.is_comment,book_lesson.lesson_cost,book_lesson.teacher_group_description,book_lesson.lesson_feature,book_lesson.cost_type,book_lesson.add_description,book_lesson.teacher_description,book_lesson.classroom_description,book_lesson.lesson_name,book_lesson.adapt_crowd,book_lesson.cost_type_description,book_lesson.lesson_time_description,book_lesson.teacher_group_images,book_lesson.lesson_num,book_city.city,book_lesson_comment.comment_status,book_order.order_sn')
            ->order('book_order.id desc')
            ->select();
        return json($order);

    }
    public function my_coin_now(){
        $orders=Db::name('order');
        $order = $orders
            ->where('book_order.user_id="'.$_POST['id'].'" AND book_lesson.lesson_num>book_lesson.com_lesson_num AND book_lesson.cost_type=2')
            ->join('book_lesson','lesson_id=book_lesson.id','LEFT')
            ->join(' book_subject ','subject_id=book_subject.id','LEFT')
            ->join(' book_lesson_program ','program_id=book_lesson_program.id','LEFT')
            ->join(' book_service_target ','service_target_id=book_service_target.id','LEFT')
            ->join(' book_lesson_nature ','lesson_nature_id=book_lesson_nature.id','LEFT')
            ->join(' book_user ','teacher_id=book_user.id','LEFT')
            ->join(' book_classroom ','classroom_id=book_classroom.id','LEFT')
            ->join(' book_city ','book_lesson.city_id=book_city.id','LEFT')
            ->join('book_lesson_num_time','book_lesson_num_time.lesson_id=book_lesson.id AND book_lesson_num_time.lesson_number  =book_lesson.lesson_num','LEFT')
            ->field('book_lesson.id,book_lesson.lesson_sn,book_subject.subject,book_lesson_program.program,book_service_target.service_target,book_lesson_nature.lesson_nature,book_user.full_name,book_user.sex,book_user.age,book_classroom.classroom_address,book_lesson.lesson_method,book_lesson.is_comment,book_lesson.lesson_cost,book_lesson.teacher_group_description,book_lesson.lesson_feature,book_lesson.cost_type,book_lesson.add_description,book_lesson.teacher_description,book_lesson.classroom_description,book_lesson.lesson_name,book_lesson.adapt_crowd,book_lesson.cost_type_description,book_lesson.lesson_time_description,book_lesson.teacher_group_images,book_lesson.lesson_num,book_city.city,book_order.order_sn')
            ->order('book_order.id desc')
            ->select();
        return json($order);

    }
    public function my_lesson_end(){
        $orders=Db::name('order');
        $order = $orders
            ->where('book_order.user_id="'.$_POST['id'].'" AND book_lesson.lesson_num=book_lesson.com_lesson_num' )
            ->join('book_lesson','lesson_id=book_lesson.id','LEFT')
            ->join(' book_subject ','subject_id=book_subject.id','LEFT')
            ->join(' book_lesson_program ','program_id=book_lesson_program.id','LEFT')
            ->join(' book_service_target ','service_target_id=book_service_target.id','LEFT')
            ->join(' book_lesson_nature ','lesson_nature_id=book_lesson_nature.id','LEFT')
            ->join(' book_user ','teacher_id=book_user.id','LEFT')
            ->join(' book_classroom ','classroom_id=book_classroom.id','LEFT')
            ->join(' book_city ','book_lesson.city_id=book_city.id','LEFT')
            ->join('book_lesson_comment','user_id=book_lesson_comment.comment_id AND book_order.lesson_id  =book_lesson_comment.lesson_id','LEFT')
            ->join('book_lesson_num_time','book_lesson_num_time.lesson_id=book_lesson.id AND book_lesson_num_time.lesson_number  =book_lesson.lesson_num','LEFT')
            ->field('book_lesson.id,book_lesson.lesson_sn,book_subject.subject,book_lesson_program.program,book_service_target.service_target,book_lesson_nature.lesson_nature,book_user.full_name,book_user.sex,book_user.age,book_classroom.classroom_address,book_lesson.lesson_method,book_lesson.is_comment,book_lesson.lesson_cost,book_lesson.teacher_group_description,book_lesson.lesson_feature,book_lesson.cost_type,book_lesson.add_description,book_lesson.teacher_description,book_lesson.classroom_description,book_lesson.lesson_name,book_lesson.adapt_crowd,book_lesson.cost_type_description,book_lesson.lesson_time_description,book_lesson.teacher_group_images,book_lesson.lesson_num,book_city.city,book_lesson.is_reclass,book_lesson_num_time.lesson_end_time')
            ->order('book_order.id desc')
            ->select();
        return json($order);

    }
    public function check_comment(){
        $id=3;
        //$id=$_POST['id'];
        $comment=Db::name('lesson_comment')
            ->where(array('comment_id'=>$id,'comment_status'=>0 ))
            ->select();
        foreach($comment as $k => $v){
          if (date('Ymd',$comment[$k]['comment_start_time'])!=date('Ymd',time())){
              dump(date('Ymd',$comment[$k]['comment_start_time']));
              $com[$k]['comment_status'] = 1;
              Db::name('lesson_comment')->where(array('comment_id' => $comment[$k]['comment_id'], 'lesson_id' => $comment[$k]['lesson_id']))->update($com[$k]);
          }

        }
    }
    public function notification(){//评论提醒
        $status0['status']=0;
        $status1['status']=1;
            if(request()->isPost()){
                $id = $_POST['id'];
                $comments=Db::name('lesson_comment');
                $com=$comments
                    ->where(array('comment_id'=>$id,'comment_status'=>0 ))
                    ->select();
                $comment=Db::name('lesson_comment')
                    ->where(array('comment_id'=>$id,'comment_status'=>0 ))
                    ->select();
                foreach($comment as $k => $v){
                    if (date('Ymd',$comment[$k]['comment_start_time'])!=date('Ymd',time())){
                        $com[$k]['comment_status'] = 1;
                        Db::name('lesson_comment')->where(array('comment_id' => $comment[$k]['comment_id'], 'lesson_id' => $comment[$k]['lesson_id']))->update($com[$k]);
                    }

                }
                if(!empty($com)){
                    return json($status0);
                }else{
                    return json($status1);
                }
            }

    }
    //查询捐赠历史**
    public function select_donation_history(){
        $donation = Db::name('donation')
            ->join('book_user','donation_id=book_user.id ','LEFT')
            ->where('book_user.tel ="'.$_POST['tel'].'" AND donation_collect_id="'.$_POST['id'].'"')
            ->field('book_user.tel,book_user.full_name,donation_price,get_coin,donation_time,donation_book')
            ->order('book_donation.id desc')
            ->select();
        return json($donation);
    }

    //添加评论信息
    public function add_comment(){
        $status0['status']=0;
        $status1['status']=1;
            $data['comment_rank']=$_POST['comment_rank'];
            $data['comment_content']=$_POST['comment_content'];
            $data['comment_status']=1;
        $comment=Db::name('lesson_comment')
            ->where('lesson_id="'.$_POST['lid'].'" AND comment_id="'.$_POST['id'].'"')
            ->update($data);
        if($comment){
            return json($status0);
        }else{
            return json($status1);
        }
    }

    //查询关注信息
    public function follows(){
       // $student_id = 3;
        if(request()->isPost()) {
            $student_id = $_POST['student_id'];
            $student = Db::name('student');
            $follow_lessons = $student
                ->join('book_user', 'student_id=book_user.id', 'LEFT')
                ->field('book_student.id as id,student_id,book_user.full_name,follow_les_id')
                ->where('student_id=' . $student_id)
                ->find();
            $les_de_ids = explode(',', $follow_lessons['follow_les_id']);
            $les_de_id = array_reverse($les_de_ids);
            if (!empty($follow_lessons['follow_les_id'])) {
                foreach ($les_de_id as $key => $val) {
                    $lesson_details[$key] = Db::name('lesson')
                        ->join('book_subject', 'subject_id=book_subject.id', 'LEFT')
                        ->join('book_classroom', 'classroom_id=book_classroom.id', 'LEFT')
                        ->join('book_lesson_program', 'program_id=book_lesson_program.id', 'LEFT')
                        ->field('book_subject.subject,book_lesson_program.program,book_classroom.classroom_address,book_lesson.lesson_sn,book_lesson.cost_type,book_lesson.lesson_cost,book_lesson.id as lid,book_lesson_program.program_full_name,book_lesson.lesson_name')
                        ->where('book_lesson.id=' . $les_de_id[$key])
                        ->find();
                }
                return json($lesson_details);
            } else {
                $lesson_details = [];
                return json($lesson_details);

            }
        }
    }
    //学生奖金转入学费
    public function rec_bonus_to_tuition(){
        $status0['status']=0;
        $status1['status']=1;
        $status2['status']=2;
        $id=$_POST['id'];
        $user = Db::name('user')
            ->where('id='.$id)
            ->find();
        if($user['rec_bonus'] > 0){
            $student = Db::name('student');
            $students = $student->where('student_id='.$id)->find();
            $data_rec_bonus['rec_bonus'] = 0;
            $data['tuition'] = $user['rec_bonus'] + $students['tuition'];
            //dump($students['tuition']);exit;
            $res = $student->where('student_id='.$id)->update($data);
            if($res){
                $users = Db::name('user');
                $result = $users->where('id='.$id)->update($data_rec_bonus);
                if($result){
                    $account = new Account();
                    $account1['account_id'] = $id;
                    $account1['cost_description'] ='奖金转入学费+'. $user['rec_bonus'];
                    $account1['record_time'] = time();
                    $account1['cost_type'] = 2;
                    $rec1=$account->insert($account1);
                    $account2['account_id'] = $id;
                    $account2['cost_description'] ='奖金转入学费-'. $user['rec_bonus'];
                    $account2['record_time'] = time();
                    $account2['cost_type'] = 4;
                    $rec2=$account->insert($account2);
                        if($rec2){
                            return json($status0);
                        }else{
                            return json($status1);
                            //echo '<script>alert(\'录入学生推荐奖金转学费记录失败！\');history.back(-1);</script>';
                        }
                }else{
                    return json($status1);
                   // echo '<script>alert(\'扣除推荐奖金失败！\');history.back(-1);</script>';
                }
               // echo '<script>alert(\'奖金转入学费成功！\');window.location.href=\'' . U('Student/withdrawals') . '\';</script>';
            }else{
                return json($status1);
               // echo '<script>alert(\'奖金转入学费失败！\');history.back(-1);</script>';
            }
        }else{
            return json($status2);
            //echo '<script>alert(\'你没有可转入学费余额的奖金！\');history.back(-1);</script>';
        }
    }

    //学生申请提现
    public function apply_withdrawals(){
        $status0['status']=0;
        $status1['status']=1;
        $status2['status']=2;
        $status3['status']=3;
        $id=$_POST['id'];
        $student = Db::name('student')
            ->join('book_user','book_student.student_id=book_user.id','LEFT')
            ->where('book_student.student_id='.$id)
            ->select();
        if(request()->isPost()){
            $withdrawals = Db::name('student_withdrawals');
            $results = $withdrawals->where('stu_id="'.$id.'" AND student_withdrawals_status=0')->select();
            if(!empty($results)){
                return json($status3);
                //echo '<script>alert(\'申请学费提现失败，你还有申请的提现正在处理中！\');history.back(-1);</script>';
            }else{
                if(input('post.tuition') > 0 && input('post.tuition') <= $student[0]['tuition']){
                    $stu = Db::name('student');
                    $data['tuition'] = $student[0]['tuition'] - $_POST['tuition'];
                    $result = $stu->where('student_id='.$id)->update($data);
                    if($result){
                        $stu_withdrawals = new StudentWithdrawals();
                        $data['stu_id'] = $id;
                        $data['student_withdrawals_money'] = $_POST['tuition'];
                        if($stu_withdrawals->create()){
                            if($stu_withdrawals->data($data)->insert()){
                                $account = new Account();
                                $datas['account_id'] = $student[0]['student_id'];
                                $datas['cost_description'] = -$_POST['tuition'];
                                $datas['record_time'] = time();
                                $datas['cost_type'] = 2;
                                if($account->create()){
                                    if($account->data($datas)->insert()){

                                    }else{
                                       // echo '<script>alert(\'录入学生学费提现流水记录失败！\');history.back(-1);</script>';
                                    }
                                }
                            }else{
                               // echo '<script>alert(\'插入数据到申请学费提现列表失败！\');history.back(-1);</script>';
                            }
                        }
                        return json($status0);
                        //echo '<script>alert(\'申请学费提现成功！\');window.location.href=\''.U('Student/withdrawals').'\';</script>';
                    }else{
                        return json($status1);
                        // echo '<script>alert(\'申请学费提现失败！\');history.back(-1);</script>';
                    }
                }else{
                    return json($status2);
                    //echo '<script>alert(\'你输入的提现金额不正确！\');history.back(-1);</script>';
                }
            }
        }

    }
    //学生添加关注的课程
    public function add_follow_lesson(){
        $status0['status']=0;
        $status1['status']=1;
        $status2['status']=2;
        $id=$_POST['id'];
        $student = Db::name('student');
        $follow_lesson = Db::name('student')->where('student_id='.$id)->find();
        if(request()->isPost()){
            if(empty($follow_lesson['follow_les_id'])){
                $str = $_POST['lesson_details_id'];
                $data['follow_les_id'] = $str;
                $res = $student->where('student_id='.$id)->update($data);
                if($res){
                        return json($status0);
                }else{
                    return json($status1);
                }
            }else if(!empty($follow_lesson['follow_les_id'])){
                $arr = explode(',',$follow_lesson['follow_les_id']);
                if(in_array($_POST['lesson_details_id'],$arr)){
                    return json($status2);
                }else{
                    array_push($arr,$_POST['lesson_details_id']);
                    $str = implode(',',$arr);
                    $data['follow_les_id'] = $str;
                    $res = $student->where('student_id='.$id)->update($data);
                    if($res){
                        return json($status0);
                    }else{
                        return json($status1);
                    }
                }
            }
        }
    }
    //学生取消关注的课程
    public function cancel_follow_lesson(){
        $status0['status']=0;
        $status1['status']=1;
        $id=$_POST['id'];
       // return json($_POST);
           $student = Db::name('student');
          $follow_lesson = Db::name('student')->where('student_id='.$id)->find();
         if(request()->isPost()){
              $arr = explode(',',$follow_lesson['follow_les_id']);
              foreach($arr as $key => $val){
                  if($_POST['lesson_details_id'] == $arr[$key]){
                      unset($arr[$key]);
                      $str = implode(',',$arr);
                      $data['follow_les_id'] = $str;
                      $res = $student->where('student_id='.$id)->update($data);
                      if($res){
                          return json($status0);
                      }else{
                          return json($status1);
                      }
                  }else if($_POST['lesson_details_id'] !== $arr[$key]){
                      //echo '<script>alert(\' 未知错误，你没有关注过该课程！\');history.back(-1);</script>';
                  }
              }
          }
    }

    // 学生发布停课
    public function add_stu_stop_les(){
        $status0['status']=0;
        $status1['status']=1;
        if(request()->isPost()){
            $les_id=json_decode($_POST['lesson_details_id']);
            foreach($les_id as $k =>$v){
                $data['stu_id']=input("post.student_id");
                $data['les_id']=$les_id[$k];
                //$data['stu_content']=input("post.stu_content");
                $rec=Db::name('student_stop_lesson')->insert($data);
            }
            if($rec){
                return json($status0);
               // echo '<script>alert(\'申请正在处理！\');window.location.href=\''.U('Lesson/student_stop_lesson').'\';</script>';
            }else{
                return json($status1);
                //echo '<script>alert(0)</script>';
            }


        }
    }


    // 学生重上课程
    public function add_reclass(){
        $status0['status']=0;
        $status1['status']=1;
        if(request()->isPost()){
                $rec=Db::name('reclass')->insert($_POST);
                if($rec){
                    return json($status0);
                    //echo '<script>alert(\'申请成功\');window.location.href=\''.U('Lesson/index').'\';</script>';
                }else{
                    return json($status1);
                    //echo '<script>alert(\'申请失败\');history.back(-1);</script>';
                }

        }
    }


}